package cn.edu.swufe.surverpark.listener;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletContext;

import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ServletContextAware;

import cn.edu.swufe.surverpark.model.Right;
import cn.edu.swufe.surverpark.struts2.service.RightService;

/**
 * @author 陈龙 E-mail:529914446@qq.com
 * @version创建时间:2015年5月7日上午2:09:04 初始化权限监听器,在spring初始化完成后立即调用
 */
@Component
@SuppressWarnings("rawtypes")
public class InitRightListener implements ApplicationListener,
		ServletContextAware {

	@Resource
	private RightService ss;

	@Override
	public void onApplicationEvent(ApplicationEvent arg0) {
		if (arg0 instanceof ContextRefreshedEvent) {
			List<Right> rights = ss.findAllEntities();
			Map<String,Right> map = new HashMap<String,Right>();
			for(Right r:rights)
				map.put(r.getRightUrl(),r);
			if(sc != null){
				sc.setAttribute("all rights map",map);
				System.out.println("初始化所有权限到Application中!!!");
			}
		}

	}

	private ServletContext sc;
	@Override
	public void setServletContext(ServletContext arg0) {
		this.sc = arg0;
	}			
}
